/**
* @CopyRight xinweitech.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.login;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.apache.http.Header;

import com.loopj.android.http.ResponseHandlerInterface;
import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.BaseFragment;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.mode.response.BaseResponse;
import com.tstudy.laoshibang.mode.response.GetValidateNoResponse;
import com.tstudy.laoshibang.utils.CommonUtil;
import com.tstudy.laoshibang.utils.LogUtil;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
@EFragment(R.layout.reset_password_validate)
public class ResetPasswordValidateFragment extends BaseFragment{
	final static String TAG = "reset_password_validate";
	public ResetPasswordValidateFragment() {
		super();
	}
	
	@ViewById(R.id.reset_password_validate_root)
	LinearLayout mRootLayout;
	
	@ViewById(R.id.reset_password_validate_mobile)
	EditText mMobileExt;
	@ViewById(R.id.reset_password_validate_ext)
	EditText mValidateExt;
	
	@ViewById(R.id.reset_password_validate_action)
	TextView mResetAction;
	
	@ViewById(R.id.reset_password_validate_get)
	Button mVerifyCodeGet;
	
	@StringRes(R.string.regist_validateno_sending)
	String verifySendingStr;
	
	
	boolean mIsFirstLoad = true;
	Handler mHandler;
	LayoutInflater mInflater;
	String mMobile;
	String mValidateNo;
	String mNewPassword;
	int totalSecond = 60;
	int remainSecend = totalSecond;
	int mVerifyState;
	public static void add(int containerViewId) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,ResetPasswordValidateFragment_.class.getName(),args);
		((BaseActivity) BaseApplication.mCurrentActivity).add(containerViewId, fragment, TAG);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mHandler = new Handler();
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@AfterViews
	public void initView(){
		mRootLayout.setOnClickListener(null);
	}
	
	@Click({R.id.reset_password_validate_back,R.id.reset_password_validate_action,R.id.reset_password_validate_get})
	public void actionClick(View view){
		switch (view.getId()) {
		case R.id.reset_password_validate_back:
			backAction(mFragmentId);
			break;
		case R.id.reset_password_validate_action:
			resetPasswordValidate();
			break;
		case R.id.reset_password_validate_get:
			switch (mVerifyState) {
			case CONSTANT.VERIFY_STATE_UNSEND:
				getVerifyCode();
				break;
			case CONSTANT.VERIFY_STATE_RESEND:
				getVerifyCode();
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		showSoftKeyBoard(mMobileExt, false);
		showSoftKeyBoard(mValidateExt, false);
	}
	
	public void resetPasswordValidate(){
		if (TextUtils.isEmpty(mMobileExt.getText())) {
			BaseApplication.showToast(R.string.mobile_not_null);
			return;
		}else if (TextUtils.isEmpty(mValidateExt.getText())) {
			BaseApplication.showToast(R.string.validate_no_not_null);
			return;
		}else{
			mMobile = mMobileExt.getText().toString();
			mValidateNo = mValidateExt.getText().toString();
		}
		if (!CommonUtil.checkMobile(mMobile)) {
			BaseApplication.showToast(R.string.regist_error_mobile_illegal);
			return;
		}
		BaseJsonHandler<BaseResponse> response = new BaseJsonHandler<BaseResponse>() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, BaseResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (CommonUtil.responseSuccess(response)) {
					BaseApplication.showToast("验证成功");
//					backAction(mFragmentId);
//					ModifyPasswordFragment.add(mMobile,mValidateNo,mFragmentId);
					ResetPasswordFragment.add(mMobile, mValidateNo,mFragmentId);
				} else {
                    BaseApplication.showToast("验证失败:" + response.getErrMsg());
                }
			}
			@Override
			protected BaseResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return mGson.fromJson(rawJsonData,BaseResponse.class);
			}
			
		};
		HttpManager.getInstance().resetPasswordValidate(mMobile, mValidateNo,BaseApplication.mResetPasswordSmsId,response);
	}
	

	public void getVerifyCode(){
		if (TextUtils.isEmpty(mMobileExt.getText()) || !CommonUtil.checkMobile(mMobileExt.getText().toString())) {
			BaseApplication.showToast(R.string.regist_error_mobile_illegal);
			return;
		}
		CommonUtil.umengEvent(CONSTANT.EVENT_ID.VALIDATE,CONSTANT.VALIDATE_EVENT_TYPE_RESET, CONSTANT.VALIDATE_EVENT_TYPE_RESET);
		mMobile = mMobileExt.getText().toString();
		ResponseHandlerInterface response = new BaseJsonHandler<GetValidateNoResponse>() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, GetValidateNoResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (CommonUtil.responseSuccess(response)) {
					BaseApplication.mResetPasswordSmsId = response.data.smsId;
					mVerifyState = CONSTANT.VERIFY_STATE_SENDING;
					mHandler.postDelayed(updateTimeTask, 0);
				} else {
                    BaseApplication.showToast(response.getErrMsg());
                }
			}
			@Override
			protected GetValidateNoResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return mGson.fromJson(rawJsonData,GetValidateNoResponse.class);
			}
		};
		
		HttpManager.getInstance().getValidateNo(mMobile, CONSTANT.VALIDATE_NO_TYPE_FORGET_PASSWORD, response);
	}
	
	Runnable updateTimeTask = new Runnable() {
		@Override
		public void run() {
			remainSecend--;
			if (remainSecend == 0) {
				mVerifyCodeGet.setEnabled(true);
				mVerifyCodeGet.setText(R.string.regist_validateno_resend);
				mVerifyState = CONSTANT.VERIFY_STATE_RESEND;
				remainSecend = totalSecond;
//				mHandler.removeCallbacks(updateTimeTask);
			}else{
				mVerifyCodeGet.setEnabled(false);
				mVerifyCodeGet.setText(String.format(verifySendingStr, remainSecend));
				mHandler.postDelayed(updateTimeTask, 1000);
			}
		}
	};

	@Override
	public void clear() {
		if (mValidateExt != null) {
			showSoftKeyBoard(mValidateExt,false);
			mValidateExt.setText("");
		}
		if (mHandler != null && updateTimeTask != null) {
			mHandler.removeCallbacks(updateTimeTask);
		}
		if (mVerifyCodeGet != null) {
			mVerifyCodeGet.setText(R.string.regist_validateno_action);
		}
		mVerifyState = CONSTANT.VERIFY_STATE_UNSEND;
		showSoftKeyBoard(mMobileExt, false);
		showSoftKeyBoard(mValidateExt, false);
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
		}
	}

}
